﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WlanDesign
{
    public partial class FormAltPassword : Form
    {
        public FormAltPassword()
        {
            InitializeComponent();
        }

        private void FormAltPassword_FormClosing(object sender, FormClosingEventArgs e)
        {
            FormStartup.formAltPasswordstatus = true;
        }

        private void btnCommit_Click(object sender, EventArgs e)
        {
            bool bol = false;
            string OriUserNames = FormStartup.ReadIniTxt(FormStartup.IniFileName, "login", "username");//读取原ini文件中所有的用户名组成的字符串
            string OriUserPasswords = FormStartup.ReadIniTxt(FormStartup.IniFileName, "login", "userpass");//读取原ini文件中所有的用户密码组成的字符串
            string UserName = txtSubUser.Text.Trim();//输入的用户名（去掉空格）
            string UserPassword = txtSubOldPas.Text.Trim();
            string UserNewPassword = txtSubNewPas.Text.Trim();
            if (UserName == "")
            {
                MessageBox.Show("用户名不能为空，请输入用户名！");
                return;
            }
            if (UserPassword == "")
            {
                MessageBox.Show("旧密码不能为空，请输入旧密码！");
                return;
            }
            if (UserNewPassword == "")
            {
                MessageBox.Show("新密码不能为空，请输入新密码！");
                return;
            }
            if (UserPassword.Length<6||UserNewPassword.Length < 6)
            {
                MessageBox.Show("密码不能低于6位，请重新输入密码！");
                return;
            }
            UserPassword = security.MD5Encrypt(UserPassword, FormStartup.m, FormStartup.n);//输入的用户密码（去掉空格）注意，后面两个数字要与导入的加密程序一致
            UserNewPassword = security.MD5Encrypt(UserNewPassword, FormStartup.m, FormStartup.n);
            string[] iniUserNames = OriUserNames.Split(',');//用‘，’将ini文件中提取的用户名字符串分片，提取单个用户名
            string[] iniUserPassword = OriUserPasswords.Split(',');//用‘，’将ini文件中提取的用户名字符串分片，提取单个用户密码

            //判断用户输入的用户名和用户密码在ini文件中是否存在
            for (int i = 0; i < iniUserNames.Length; i++)
            {
                if (iniUserNames[i] == UserName && iniUserPassword[i] == UserPassword)
                {
                    iniUserPassword[i] = UserNewPassword;
                    bol = true;
                    break;
                }
            }
            if (bol == true)
            {
                string userNames = iniUserNames[0];
                string userPasswords = iniUserPassword[0];
                for (int i = 1; i < iniUserNames.Length; i++)
                {
                    userNames = userNames + "," + iniUserNames[i];
                    userPasswords = userPasswords + "," + iniUserPassword[i];
                }
                FormStartup.WriteIniTxt(FormStartup.IniFileName, "login", "username", userNames);//在ini文件中写入用户名
                FormStartup.WriteIniTxt(FormStartup.IniFileName, "login", "userpass", userPasswords);//在ini文件中写入用户密码
                MessageBox.Show("密码修改成功！");
            }
            //如不存在此用户，或者密码错误，提示相应信息
            else
            {
                MessageBox.Show("用户名或密码不正确，请重新输入！");
            }
        }
    }
}
